package com.stone.business.sbqxjl.action;

/**
 * <pre>
 * 设备缺陷记录
 * @author
 * </pre>
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.sbqxjl.pojo.TSbqxjl;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.util.SpringUtil;

public class SbqxjlListAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;

	private ZbjlCommonService myService;
	
	public ZbjlCommonService getMyService() {
		return myService;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public SbqxjlListAction() {
        this.entityClass = TSbqxjl.class;
    }
    
	// 设备类型
	private List<TDictionary> sblxList;
	
	// 缺陷类别
	private List<TDictionary> qxlbList;
	HashMap<String, Object> m = new HashMap<String, Object>();
	/**
	 * 设置查询条件
	 */
	public HashMap<String, Object> setSearchCondition() {
		
		//HashMap<String, Object> m = new HashMap<String, Object>();
		
		List<Object> valsList = new ArrayList<Object>();

		m.put("cols", "new map(s.id as id, s.fxrq as fxrq, s.xcsj as xcsj,s.dyfxrname as dyfxrname, s.qxnr as qxnr, s.qxly as qxly,s.bdzname as bdzname, s.xcyy as xcyy, dic.itemName as qxlbname,"
		+ "(select count(*) from TFile f where f.tableid = s.id and f.colname ='TAB_SBQXJL-QXZP') as filecount)");
//		// 登录者的organCode 100001
	String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
//		// 是否管理员
		String isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
//		// 查询条件OUID很重要
		String conditions ="";
		if (!"1".equals(isAdmin)&&!roles.startsWith("GQLD")) {
			conditions += " s.ouid like '%," + organCode + "%,%'";
	} else {
		conditions += " 1=1 ";
		}
//		by wangtao update20110315
		//String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
	//	String isLike = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_ISLIKE));
//		 查询条件OUID很重要
		//String conditions = " s.ouid "+isLike+" '%," + organCode + "%,%'";
	//	String conditions = " s.ouid like "+" '%," + organCode + "%,%'";
		 conditions += " and s.qxlb = dic.itemId and dic.itemType = 'QXLB' and state = '" + super.getSearchBean().getCon1() + "'"; 
		// 状态
			if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())) {
				conditions += " AND s.state = ?";
				valsList.add(super.getSearchBean().getCon1());
			}
		// 发现时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND TO_CHAR(s.fxrq,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND TO_CHAR(s.fxrq,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		/*// 变电站
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
			conditions += " AND s.bdzname like ?";
			valsList.add("%" + super.getSearchBean().getCon4()+ "%");
		}*/
		
		// 缺陷类别
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
			conditions += " AND s.qxlb = ?";
			valsList.add(super.getSearchBean().getCon7());
		}
		// 变电站 by wangtao add20120223
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
			conditions += " AND s.bdzname in (";
			StringBuffer buf= new StringBuffer(""); 
			String[] bdznames = ((String) super.getSearchBean().getCon4() == null? "" : super.getSearchBean().getCon4()).split(",");
			
			for (int i=0; i< bdznames.length; i++) { 
			  if (i!=0) buf.append(", "); 
			  buf.append("?"); 
			  valsList.add("" + bdznames[i]+ "");
			  //System.out.println("bdznames["+i+"]=="+bdznames[i]);
			} 
			buf.append(")"); 
			conditions +=buf;

			
		}
		// 查询条件
		m.put("conditions",conditions);
		m.put("clause", " order by s.fxrq Desc,s.bdzname");
		m.put("alias", "s, TDictionary dic");//, TOrgan o
		m.put("vals", valsList.toArray());
		
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		sblxList = dicService.findList("SBLX");
		
		// 缺陷类别
		qxlbList = dicService.findList("QXLB");
		
		return m;
		
	}
	/**
	 * 生成csv
	 */
	@Override
	public HashMap<String, Object> createCsvMap() {
		this.setSearchCondition();
		m.put("title", "设备缺陷记录");
		m.put("searchcols", "s.fxrq as fxrq, s.dyfxrname as dyfxrname,s.qxnr as qxnr, s.id as id,dic.itemName as qxlbname, s.bdzname as bdzname, s.xcyy as xcyy,(select count(*) from TFile f where f.tableid = s.id and f.colname ='TAB_SBQXJL-QXZP') as filecount");
		m.put("colscn", "发现日期,变电站,发现人 ,缺陷类别,缺陷照片,缺陷内容 ,原因及处理情况");
		m.put("colsen", new String[] {"fxrq","bdzname","dyfxrname","qxlbname","filecount","qxnr","gzrw"});
		m.put("classname", TSbqxjl.class.getName() + " s, TDictionary dic");
		
		return m;
	}
	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public List<TDictionary> getQxlbList() {
		return qxlbList;
	}



}
